﻿using System;
using System.Collections.Generic;
using System.IO;
using PresentationNamespace;

namespace Infrastructure
{
    public class HtmlLogger:ILogger
    {
        private List<string> logMessages;

        public HtmlLogger()
        {
            logMessages = new List<string>();
        }

        public HtmlLogger(List<string> messages)
        {
            logMessages = new List<string>(messages);
        }

        public List<string> GetLogMessages()
        {
            var htmlTxt = new List<string>();
            
            htmlTxt.Add("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body><table>");
            foreach (var message in logMessages)
            {
                htmlTxt.Add(string.Format("<tr><td>{0}</td></tr>", message));
            }
            htmlTxt.Add("</table></body></html>");

            return htmlTxt;
        }

        public void AddMessage(string message)
        {
            logMessages.Add(message);            
        }

        public void WriteMessages(string fileName)
        {
            var htmlTxt = GetLogMessages();
            try
            {
                var writer = new StreamWriter(fileName, false);
                foreach (var message in htmlTxt)
                {
                    writer.WriteLine(message);
                }
                writer.Close();
            }
            catch (Exception)
            {
                throw new FileNotFoundException();
            }
        }

        public List<string> GetLoggerHistory()
        {
            return logMessages;
        }

        public void ClearLog()
        {
            logMessages.Clear();
        }

    }
}
